package com.nl.controller.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.SmsCounty;
import com.nl.controller.web.bean.SmsMgrTemplateInfo;
import com.nl.controller.web.bean.SmsReportDetail;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.service.SmsMgrTemplateInfoService;
import com.nl.controller.web.service.SmsReportDetailService;
import com.nl.controller.web.service.SmsUserService;
import com.nl.controller.xuyi.service.XuYiStationService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@RequestMapping("/util")
public class UtilController {
    @Autowired
    private SmsUserService smsUserService;
    @Autowired
    private SmsReportDetailService smsReportDetailService;
    @Autowired
    private SmsMgrTemplateInfoService smsMgrTemplateInfoService;
    
    
    
    @ApiOperation(value = "身份识别", notes = "身份识别")
    @PostMapping("findUser")
    public SmsUsers findUser(HttpSession session) {
        return (SmsUsers) session.getAttribute("user");
    }


    @ApiOperation(value = "短信发送明细查询", notes = "查询区域下的基站")
    @PostMapping("findStationInfo")
    private Response findStationInfo(@RequestParam(value = "regionId") String regionId, @RequestParam(value = "cityId") String cityd, @RequestParam(value = "mgrId") String mgrId) {
        List<SmsReportDetail> list = smsReportDetailService.findStationInfo(regionId, cityd, mgrId);
        log.info("查询区域下的基站：{}", mgrId);
        log.debug("查询区域下的基站返回数据：{}", list.size());
        return ResponseUtil.sucess(list);
    }

    @ApiOperation(value = "短信发送明细查询", notes = "查询项目下的区域")
    @PostMapping("findRegionInfo")
    private Response findRegionInfo(@RequestParam(value = "mgrId") String mgrId,
                                    @RequestParam(value = "cityId") String cityId) {
        List<SmsReportDetail> list = smsReportDetailService.findRegionInfo(mgrId, cityId);
        log.info("查询项目下的区域：{}", mgrId);
        log.debug("查询项目下的区域返回数据：{}", list.size());
        return ResponseUtil.sucess(list);
    }

    
    @ApiOperation(value = "项目维护管理", notes = "根据城市查询扩展码信息")
    @ApiImplicitParam(name = "cityId", value = "城市编号", required = true, dataType = "String")
    @PostMapping("findExdId")
    public Response<String> findExtIdByCityId(
            @RequestParam(value = "cityId") String cityId
    ) {
        log.info("根据城市查询扩展码信息参数：{}", cityId);
        List<String> list = new ArrayList<>();
        if (cityId != null && !"undefined".equals(cityId) && cityId != "") {
            list = smsUserService.findExtIdByCityId(cityId);
        }
        log.debug("根据城市查询扩展码信息返回数据：{}", list.size());
        return ResponseUtil.sucess(list);
    }
	
	@ApiOperation(value = "项目维护管理", notes = "根据城市查询区县信息")
    @ApiImplicitParam(name = "cityId", value = "城市编号", required = true, dataType = "String")
    @PostMapping("findCountyByCityId")
    public Response<SmsCounty> findCountyByCityId(@RequestParam(value = "cityId") String cityId) {
        log.info("根据城市查询区县信息参数：{}", cityId);
        List<SmsCounty> list = new ArrayList<SmsCounty>();
        if (cityId != null && !"undefined".equals(cityId) && cityId != "") {
            list = smsUserService.findCountyByCityId(cityId);
        }
        log.debug("根据城市查询区县信息返回数据：{}", list.size());
        return ResponseUtil.sucess(list);
    }
	
    @ApiOperation(value = "用户管理模板", notes = "根据城市查询项目信息")
    @ApiImplicitParam(name = "cityId", value = "城市编号", required = true, dataType = "String")
    @PostMapping("findByCityId")
    public Response<SmsMgrTemplateInfo> findOperatorByCityId(
            @RequestParam(value = "cityId") String cityId,HttpSession session
    ) {
    	 SmsUsers user = (SmsUsers) session.getAttribute("user");
         String duty = user.getDuty();
         String msisdn = user.getMsisdn();
        log.info("根据城市查询项目信息参数：{}", cityId);
        List<SmsMgrTemplateInfo> list = new ArrayList<>();
        if (cityId != null && !"undefined".equals(cityId) && cityId != "") {
        	 list = smsUserService.findMgrByCityId(cityId,duty,msisdn);
        }
        log.debug("根据城市查询项目信息返回数据：{}", list.size());
        return ResponseUtil.sucess(list);
    }
	
    @ApiOperation(value = "项目维护管理", notes = "根据区县查询项目")
    @ApiImplicitParam(name = "countyId", value = "区县编号", required = true, dataType = "String")
    @PostMapping("findByMgrId")
    public Response<SmsMgrTemplateInfo> findByMgrId(
            @RequestParam(value = "countyId") String countyId,HttpSession session) {
        log.info("根据区县查询项目参数：{}", countyId);
        SmsUsers user = (SmsUsers) session.getAttribute("user");
        String duty = user.getDuty();
        String msisdn = user.getMsisdn();
        List<SmsMgrTemplateInfo> list = new ArrayList<SmsMgrTemplateInfo>();
        if (countyId != null && !"undefined".equals(countyId) && countyId != "") {
            if(countyId.length()>2){
                list = smsUserService.findMgrByCountyId(countyId,duty,msisdn);
            }else if(countyId.length()==2){
                list = smsUserService.findMgrByCityId(countyId,duty,msisdn);
            }
        }
        log.debug("根据城市查询项目信息返回数据：{}", list.size());
        return ResponseUtil.sucess(list);
    }
	
    @ApiOperation(value = "获取项目序列号", notes = "获取项目序列号")
    @PostMapping("getMgrId")
    public Response<String> findExtIdByCityId() {
        String mgrId = smsMgrTemplateInfoService.getMgrId();
        log.debug("获取项目序列号：{}", mgrId);
        return ResponseUtil.sucess(mgrId);
    }
    @ApiOperation(value = "获取区县名称", notes = "获取区县名称")
    @PostMapping("getCountyName")
    public Response<String> getCountyName( @RequestParam(value = "countyId") String countyId) {
        String countyName= smsUserService.getCountyName(countyId);
        log.debug("获取区县名称：{}", countyName);
        return ResponseUtil.sucess(countyName);
    }
    
    @ApiOperation(value = "项目维护管理", notes = "根据城市查询经分提供的区县信息")
    @PostMapping("findJFCountyByCityId")
    public Response<SmsCounty> findJFCountyByCityId(@RequestParam(value = "cityId") String cityId) {
        log.info("根据城市查询区县信息参数：{}", cityId);
        List<SmsCounty> list = new ArrayList<SmsCounty>();
        if (cityId != null && !"undefined".equals(cityId) && cityId != "") {
            list = smsUserService.findJFCountyByCityId(cityId);
        }
        log.debug("根据城市查询区县信息返回数据：{}", list.size());
        return ResponseUtil.sucess(list);
    }
    
    @ApiOperation(value = "获取项目名称是否存在", notes = "获取项目名称是否存在")
    @PostMapping("getMgrName")
    public Response<Integer> getMgrName(@RequestParam(value = "mgrName") String mgrName) {
        int flag = smsUserService.getMgrName(mgrName);
        log.debug("获取项目名称是否存在：{}", flag);
        return ResponseUtil.sucess(flag);
    }
    
    @ApiOperation(value = "短信项目管理", notes = "项目名称联想")
    @PostMapping("selectMgrNameLike")
    public Response<String> selectMgrNameLike(@RequestParam(value = "key", required = true) String key,
            @RequestParam(value = "cityId", required = false) String cityId,
            @RequestParam(value = "countyId", required = false) String countyId
    		) {
        
        log.info("项目名称联想参数：{}", key);
        List<SmsMgrTemplateInfo> list = new ArrayList<>();
        if (key != null && !"undefined".equals(key) && key != "") {
            list = smsUserService.selectMgrNameLike(key,cityId,countyId);
        }
        log.debug("项目名称联想返回数据：{}", list.size());
        return ResponseUtil.sucess(list);
    }

    public static void main(String[] args) {

    }
}
